<template>
  <el-card style="max-width: 99%">
    <template #header>
      <div class="card-header">
        <span>用户管理</span>
      </div>
    </template>
    <el-row style="margin-bottom: 20px">
      <el-col :span="5">
        <el-form :model="form" style="max-width: 200px;margin-left: 25px">
          <el-form-item label="用户名称">
            <el-input v-model="realName" placeholder="请输入用户名称" />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="19">
        <el-button type="primary" @click="cx()">搜索</el-button>
        <el-button @click="cz()">重置</el-button>
      </el-col>
    </el-row>
    <el-table :data="list" border style="margin-bottom: 15px">
      <el-table-column prop="realName" label="用户名称" width="180" />
      <el-table-column prop="phone" label="手机号" width="180" />
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          <span v-if="scope.row.sex == 1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="查看" >
        <template #default="scope">
          <el-button :icon="View" circle @click="openTc(scope.row.id)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button :icon="Select" circle @click="jd(scope.row)"/>
          <el-button :icon="CloseBold" circle  @click="dj(scope.row)"/>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 0 auto;width: 300px">
      <el-pagination @current-change="handleCurrentChange"
                     size="small" background layout="prev, pager, next" :total="count" :page-size=pageSize class="mt-4"/>
    </div>
  </el-card>

  <el-dialog v-model="dialogFormVisible" :before-close="qx" title="用户信息" width="500">
    <el-form :model="userForm" style="margin-right: 60px;margin-left: 60px">
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="userForm.nickName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="userForm.realName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="userForm.phone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="userForm.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-input v-model="userForm.birthday" autocomplete="off" />
      </el-form-item>
      <el-form-item label="职业" prop="career">
        <el-input v-model="userForm.career" autocomplete="off" />
      </el-form-item>
      <el-form-item label="QQ" prop="qq">
        <el-input v-model="userForm.qq" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qx">取消</el-button>
        <el-button type="primary" @click="qd()">确定</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script>
import {selectAllUser,selectUserById, updateUser1, updateUser2} from "@/api/vue3";
import {ElMessage} from "element-plus";
import {CloseBold, Select, View} from "@element-plus/icons-vue";
export default {
  name: 'User',
  computed: {
    CloseBold() {
      return CloseBold
    },
    SemiSelect() {
      return SemiSelect
    },
    Select() {
      return Select
    },
    CircleCloseFilled() {
      return CircleCloseFilled
    },
    CircleCheckFilled() {
      return CircleCheckFilled
    },
    View() {
      return View
    },
    Search() {
      return Search
    }
  },
  data(){
    return{
      list:[],//用户列表
      pageNum:1,//当前页
      pageSize:5,//每页显示条数
      count:0,//总条数
      dialogFormVisible:false,//对话框显示
      userForm:{},//用户信息
      realName:''
    }
  },
  mounted() {
    this.selList()
  },
  methods:{
    selList(){
      selectAllUser(this.realName,this.pageNum,this.pageSize).then(ref=>{
        this.list = ref.data.data.list
        this.count = ref.data.data.count
      })
    },
    handleCurrentChange(val) {//当前页码改变的事件
      this.pageNum = val;
      this.selList()
    },
    cx(){
      this.realName
      this.selList()
    },
    cz(){
      this.realName=''
      this.selList()
    },
    //打开弹窗
    openTc(id){
      this.dialogFormVisible= true
      selectUserById(id).then(res=>{
        console.log(res)
        this.userForm=res.data.data.data
      })
    },
    dj(data){
      updateUser2(data).then(res=>{
        if (res.data.code==200){
          ElMessage.success("已冻结")
          this.selList()
        }else{
          ElMessage.error(res.data.msg)
        }
      })
    },
    jd(data){
      updateUser1(data).then(res=>{
        if (res.data.code==200){
          ElMessage.success("已解冻")
          this.selList()
        }else{
          ElMessage.error(res.data.msg)
        }
      })
    },
    qd(){
      this.dialogFormVisible= false
    },
    qx(){
      this.dialogFormVisible= false
    }
  }
}
</script>

<style scoped>
.el-card{
  width: 100%;
  height: 600px;
  background-color: white;
  margin-top: 20px;
  border-radius: 30px;
}
.el-table{
  margin: 0 auto;
  width: 96%;
}
</style>